package tech.aistar.corejava.day17.homework;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/8/6
 * @Version: 1.0
 */
public class EmployDaoImpl implements IEmployDao {
    List<Employ> list = new ArrayList<>();
    public static final String PATH = "src/tech/aistar/corejava/day17/homework/employ.txt";


    public EmployDaoImpl() {
        Employ e1 = new Employ("1001","tom","2012-9-9","技术");
        Employ e2 = new Employ("1002","jack","2013-10-10","销售");
        Employ e3 = new Employ("1003","tom","2012-9-9","技术");
        Employ e4 = new Employ("1004","tom","2012-9-9","技术");
    }

    @Override
    public void add(Employ e) {
        list.add(e);
    }

    @Override
    //添加Employee对象到数组中,并保存到文件中
    public void sive(Employ emp) {
        for (Employ e : list) {
            if(e.getEmpId().equals(emp.getEmpId())){
                System.out.println("编号已存在");
                return;
            }
        }
        list.add(emp);
        write();
    }

    private void write() {
        File file = new File(PATH);
        PrintWriter out = null;
        if(!file.exists()){
            try {
                file.createNewFile();
                out = new PrintWriter(new FileWriter(PATH),true);
                for (Employ employ : list) {
                    out.println(toWord(employ));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (null != out){
                        out.close();
                }
            }
        }
    }

    @Override
    public List<Employ> findAll() {
        BufferedReader br = null;
        List<Employ> employs = new ArrayList<>();

        try {
            br = new BufferedReader((new FileReader(PATH)));
            while(true){
                String line = br.readLine();
                if(null == line){
                    break;
                }
                Employ e = read(line);
                employs.add(e);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    //按出生日期降序进行排序,显示所有员工信息
    public void showAll() {
        Collections.sort(list, new Comparator<Employ>() {
            @Override
            public int compare(Employ o1, Employ o2) {
                if (o1.getEmpBir().after(o2.getEmpBir())){
                    return -1;
                }else{
                    return 1;
                }
            }
        });
        list.forEach(System.out::println);
    }

    //将一行String字符串转换成Employ对象
    private Employ read(String line) {
        String regEx = "[|]";
        String[] temp = Pattern.compile(regEx).matcher(line).replaceAll(" ").trim().split(" ");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Employ employ = null;
        employ = new Employ(temp[0],temp[1],temp[2],temp[3]);
        return employ;
    }

    //将一个Employee对象转换为String字符串
    public String toWord(Employ employ){
        StringBuffer stringBuffer = new StringBuffer("");
        stringBuffer.append("|");
        stringBuffer.append(employ.getEmpId());
        stringBuffer.append("|");
        stringBuffer.append(employ.getEmpName());
        stringBuffer.append("|");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        stringBuffer.append(sdf.format(employ.getEmpBir()));
        stringBuffer.append("|");
        stringBuffer.append(employ.getEmpJob());
        return new String(stringBuffer);
    }



}
